package com.yidu.hbats.service.impl.emp;

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.dao.AtsEmpLeaderMapper;
import com.yidu.hbats.model.AtsEmpLeader;
import com.yidu.hbats.service.emp.AtsEmpLeaderService;
import com.yidu.springbootdemo.permission.domain.Tuser;
import com.yidu.springbootdemo.permission.service.TuserService;
import com.yidu.springbootdemo.permission.tools.Tools;
import com.yidu.utils.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @program: hbats
 * @description: 中心领导信息实现类
 * @author: 张超
 * @create: 2019-11-01 11:12
 */

@Service
public class AtsEmpLeaderServiceImpl implements AtsEmpLeaderService {

    @Resource
    AtsEmpLeaderMapper atsEmpLeaderMapper;

    @Override
    public int deleteAll(String ids) {
        if(StringUtils.isEmpty(ids)){
            return 0;
        }else{
            String[] idss = ids.split(",");
            for(int i = 0;i<idss.length;i++){
                AtsEmpLeader atsEmpLeader = new AtsEmpLeader();
                atsEmpLeader.setLeaderId(Integer.valueOf(idss[i]));
                atsEmpLeader.setIsva(2);
                saveOrUpdate(atsEmpLeader);
            }
            return 1;
        }
    }

    @Resource
    TuserService tuserService;

    @Override
    public int saveOrUpdate(AtsEmpLeader atsEmpLeader) {
        if(StringUtils.isEmpty(atsEmpLeader.getLeaderId())){
            atsEmpLeaderMapper.insertSelective(atsEmpLeader);
            Tuser tuser = new Tuser();
            String rd = UUID.randomUUID().toString().replaceAll("-", "");
            tuser.setUserId(rd);
            tuser.setFilId(atsEmpLeader.getLeaderId().toString());//对应对象id
            tuser.setCompanyNum("1");//类型字段 对应 1 , 领导  2,教练员 3,专家 4,运动员
            tuser.setCompanyName(atsEmpLeader.getLeaderName());//对象名称
            tuser.setCompany("领导");//对象类型名称
            tuser.setUserName(atsEmpLeader.getLeaderName());
            tuser.setUserPassword("123456");
            tuser.setUserState("Y");
            tuser.setUserFoundDate(Tools.time());
            tuser.setUserFoundUser(atsEmpLeader.getCreater());


            tuserService.add(tuser);

        }else{
            atsEmpLeaderMapper.updateByPrimaryKeySelective(atsEmpLeader);

            Tuser tuser = new Tuser();
            tuser.setFilId(atsEmpLeader.getLeaderId().toString());
            tuser.setCompanyNum("1");

            Tuser tuser1 = tuserService.findBycompanyNum(tuser);
            if(tuser1!=null){
                //如果是删除的话 就把用户表中的也删除
                if(atsEmpLeader.getIsva()!=null && atsEmpLeader.getIsva()==2){
                    if(tuser1!=null) {
                        tuserService.delete(tuser1.getUserId());
                    }
                }else {
                    tuser1.setCompanyName(atsEmpLeader.getLeaderName());//对象名称
                    tuser1.setUserName(atsEmpLeader.getLeaderName());
                    tuserService.update(tuser1);
                }
            }
        }
        return 0;
    }

    @Override
    public AtsEmpLeader findById(Integer id) {
        return atsEmpLeaderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AtsEmpLeader> selectAll(AtsEmpLeader atsEmpLeader, Page page) {
        PageHelper.offsetPage(page.getStart(), page.getRows());

        List<AtsEmpLeader> list = atsEmpLeaderMapper.selectAll(atsEmpLeader);

        PageHelper.startPage(page.getStart(), page.getRows());
        return list;

    }

    @Override
    public int selectCount(AtsEmpLeader atsEmpLeader) {
        return atsEmpLeaderMapper.selectCount(atsEmpLeader);
    }

    @Override
    public List<AtsEmpLeader> findAll(AtsEmpLeader atsEmpLeader) {
        return atsEmpLeaderMapper.findAll(atsEmpLeader);
    }
}